#include "flowmetersli1000.h"

#include <QLibrary>
#include <QMessageBox>
#include <QDebug>

#if defined Q_OS_LINUX
quint32 _OpenPort(quint8 aPortType, char *aPortConfig, quint32 *aPortHandle){}
quint32 _ClosePort(quint32 aPortHandle){}
quint32 _GetSensorPartName(quint32 aPortHandle, quint8 aSlaveAdr, char* aPartNameString, quint32 aStringMaxSize){}
const char* _TranslateErrorCode(quint32 aErrorCode){}
quint32 _StartContinuousMeasurement(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, quint16 aInterval){}
quint32 _StopContinuousMeasurement(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr){}
quint32 _GetLastMeasurementSigned(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, qint16* aMeasureResult){}
quint32 _GetMeasurementBufferSigned(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, qint16 aMeasureResult[127], quint8* aLength){}
#endif

FlowmeterSLI1000::FlowmeterSLI1000(QObject *parent)
    : BaseModule(parent)
{
    m_timer = new QTimer(this);
    connect(m_timer, &QTimer::timeout, this, &FlowmeterSLI1000::onTimeout);
}

FlowmeterSLI1000::~FlowmeterSLI1000()
{

}

bool FlowmeterSLI1000::setConfig(int id, QVariant &var)
{
    bool ret = false;
    switch (id)
    {
    case ConfigID_Open:
    {
        QString str = QString("%1, 115200, ECHOOFF").arg(var.toString());
        QByteArray ba = str.toLatin1();

        xError = OpenPort(0, ba.data(), &Connection);
        qDebug("xError:%d, %s", xError, TranslateErrorCode(xError));
        if (xError != 133)//Could not open the COM port.
        {
            char Partname[256];
            xError = GetSensorPartName(Connection, 0, Partname, 256);

            qDebug("Partname = %s\r\n", Partname);

            ret = true;
        }
        else
        {
            ret = false;
            qDebug("Could not open the COM port.\r\n");
        }
    }
        break;
    case ConfigID_Close:
    {
        xError = ClosePort(Connection);
        m_timer->stop();
        ret = true;
    }
        break;
    case ConfigID_Start:
    {
        StartContinuousMeasurement(Connection, 0, 0, 0);
        m_timer->start(20);
        ret = true;
    }
        break;
    case ConfigID_Stop:
    {
        StopContinuousMeasurement(Connection, 0, 0);
        m_timer->stop();
        ret = true;
    }
        break;
    default:
        break;
    }
    return ret;
}

bool FlowmeterSLI1000::getConfig(int id, QVariant &var)
{
    bool ret = false;
    return ret;
}

void FlowmeterSLI1000::onStart()
{
    importLib();
}

void FlowmeterSLI1000::onTimeout()
{
    qint16 flowData;
    xError = GetLastMeasurementSigned(Connection, 0, 0, &flowData);
    if (xError != 1376)
    {
//        qDebug("flowData = %d\r\n", flowData);
        emit getData(flowData);
    }
    else
    {
        qDebug("Flow Sensor is no data!\r\n");
    }
}

void FlowmeterSLI1000::importLib()
{
#if defined(_WIN32)
    QLibrary mylib("ShdlcDriver.dll");   //声明所用到的dll文件
    if (mylib.load())              //判断是否正确加载
    {
        OpenPort = (FctOpenPort)mylib.resolve("OpenPort");    //援引 add() 函数
        if (!OpenPort)
            QMessageBox::information(nullptr, "NO", QString("Linke to Function is not OK!!!! -%1").arg(mylib.errorString()));

        ClosePort = (FctClosePort)mylib.resolve("ClosePort");    //援引 add() 函数
        if (!ClosePort)
            QMessageBox::information(nullptr, "NO", QString("Linke to Function is not OK!!!! -%1").arg(mylib.errorString()));
    }
    else
        QMessageBox::information(nullptr, "NO", QString("ShdlcDriver.dll is not loaded!-%1").arg(mylib.errorString()));

    QLibrary mylib2("SensorCableDriver.dll");   //声明所用到的dll文件
    if (mylib2.load())              //判断是否正确加载
    {
        GetSensorPartName = (FctGetSensorPartName)mylib2.resolve("GetSensorPartName");    //援引 add() 函数
        if (!GetSensorPartName)
            QMessageBox::information(nullptr, "NO", QString("Linke to Function is not OK!!!! -%1").arg(mylib2.errorString()));
    }
    else
        QMessageBox::information(nullptr, "NO", QString("SensorCableDriver.dll is not loaded!-%1").arg(mylib.errorString()));

    TranslateErrorCode = (FctTranslateErrorCode)mylib2.resolve("TranslateErrorCode");
    StartContinuousMeasurement = (FctStartContinuousMeasurement)mylib2.resolve("StartContinuousMeasurement");
    StopContinuousMeasurement = (FctStopContinuousMeasurement)mylib2.resolve("StopContinuousMeasurement");
    GetLastMeasurementSigned = (FctGetLastMeasurementSigned)mylib2.resolve("GetLastMeasurementSigned");
    GetMeasurementBufferSigned = (FctGetMeasurementBufferSigned)mylib2.resolve("GetMeasurementBufferSigned");
#else
    OpenPort = _OpenPort;
    ClosePort = _ClosePort;
    GetSensorPartName = _GetSensorPartName;
    TranslateErrorCode = _TranslateErrorCode;
    StartContinuousMeasurement = _StartContinuousMeasurement;
    StopContinuousMeasurement = _StopContinuousMeasurement;
    GetLastMeasurementSigned = _GetLastMeasurementSigned;
    GetMeasurementBufferSigned = _GetMeasurementBufferSigned;
#endif
}
